package shoppingmall.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.manage.mapper.CommodityCategoryMapper;
import shoppingmall.manage.pojo.CommodityCategory;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.service.CommodityCategoryService;

import java.util.List;

@Service
public class CommodityCategoryServiceImpl implements CommodityCategoryService {

    @Autowired
    private CommodityCategoryMapper commodityCategoryMapper;

    /**
     * 分页查询商品分类数据
     */
    @Override
    public PageBean getCommodityCategoryList(int page, int pageSize) {
        //1.获取总记录数
        int commodityCategoryCount = commodityCategoryMapper.getCommodityCategoryCount();
        //2.获取分页查询结果列表
        int start = (page - 1) * pageSize;
        List<CommodityCategory> commodityCategoryList = commodityCategoryMapper.getCommodityCategoryList(start, pageSize);
        //3.封装PageBean对象
        PageBean pageBean =  new PageBean(commodityCategoryCount,commodityCategoryList);
        //4.返回结果
        return pageBean;
    }

    /**
     * 查询所有的商品分类数据
     */
    @Override
    public PageBean getAllCommodityCategoryList() {
        //1.获取总记录数
        int commodityCategoryCount = commodityCategoryMapper.getCommodityCategoryCount();
        //2.获取分页查询结果列表
        List<CommodityCategory> commodityCategoryList = commodityCategoryMapper.getAllCommodityCategoryList();
        //3.封装PageBean对象
        PageBean pageBean =  new PageBean(commodityCategoryCount,commodityCategoryList);
        //4.返回结果
        return pageBean;
    }

    /**
     * 保存一条商品分类数据
     * @param categoryName
     * @return
     */
    @Override
    public boolean saveCommodityCategory(String categoryName) {
        try {
            commodityCategoryMapper.saveCommodityCategory(categoryName);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 更新一条商品分类数据
     * @param commodityCategory
     * @return
     */
    @Override
    public boolean updateCommodityCategory(CommodityCategory commodityCategory) {
        try {
            commodityCategoryMapper.updateCommodityCategory(commodityCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除一条商品分类数据
     * @param categoryId
     * @return
     */
    @Override
    public boolean deleteCommodityCategory(int categoryId) {
        try {
            commodityCategoryMapper.deleteCommodityCategory(categoryId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 批量删除商品分类数据
     * @param categoryIds
     * @return
     */
    @Override
    public boolean deleteCommodityCategoryBatch(List<Integer> categoryIds) {
        try {
            int i = commodityCategoryMapper.deleteCommodityCategoryBatch(categoryIds);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
